package mvc_generator.generator.car.jsf

import org.eclipse.xtext.generator.IFileSystemAccess
import mvc_generator.mVCGenerator.Form
import mvc_generator.mVCGenerator.View
import mvc_generator.generator.MVCGeneratorGeneratorUtils
import mvc_generator.mVCGenerator.ModelMVC
import java.util.ArrayList
import mvc_generator.mVCGenerator.AssignedField

class ViewGenerator {
	  def void generate(View view, IFileSystemAccess fsa){
	  	var int i= 0;
	  	var ModelMVC modelMVCContainer= MVCGeneratorGeneratorUtils.findModelMVCContainer(view);
	  	var CharSequence fileContent= null;
	  	
	  	if(modelMVCContainer==null){
	  		fileContent='''@GenerationError: ModelMVC container not found for this view'''
	  		
	  		if(true){
	  			fileContent =  fileContent + new ArrayList<String>().toString();
	  		}
	  		
	  	}
	  	else{
		  	fileContent=
	    	'''
			<!DOCTYPE html PUBLIC 
			   "-//W3C//DTD XHTML 1.0 Transitional//EN"
			   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
			
			<html xmlns="http://www.w3.org/1999/xhtml"
			      xmlns:h="http://java.sun.com/jsf/html">
			      <h:head>
			      	<title>«view.name»</title>
			      </h:head>
			      <h:body>
					«IF view.forms.size == 0»
					<p>Voici une page terminal !</p>
			      	«ELSE»
					«FOR Form form : view.forms»
					<p>«form.name»</p>
					<p>  #{«view.name.toFirstLower»Controller.«form.name.toFirstLower»Error} </p>
					<h:form>
						«FOR AssignedField assignedField : form.assignedFields »
						«assignedField.fieldName»: <h:inputText value="#{«view.name.toFirstLower»Controller.«assignedField.fieldName»}" /> <br/>
						«ENDFOR» 
						<h:commandButton value="validate" action="#{«view.name.toFirstLower»Controller.validate«view.name.toFirstUpper»«form.name.toFirstUpper»}" />
					</h:form>
					«ENDFOR»
					«ENDIF»
			      </h:body>
			</html>
	    	''' 	
	    }
    	
    	fsa.generateFile(modelMVCContainer.name+"Jsf/WebContent/"+view.name+".xhtml", fileContent);
	}
}